package com.Maykk.Demo05;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ClassName: Demo02
 * @Description: TODO TCP 接收练习
 * @Author: MaykkStar@163.com
 */
public class Demo02 {
    public static void main(String[] args) throws IOException {
        // 实例化服务器端 Socket
        ServerSocket serverSocket = new ServerSocket(9527);

        // 用服务器端 Socket 对象获取 Socket 对象
        Socket socket = serverSocket.accept();

        // 获取输入流用于接收数据
        InputStream is = socket.getInputStream();

        // 接收数据
        byte[] data = new byte[1024];
        int length = is.read(data);

        System.out.println("接收到客户端数据：" + new String(data, 0, length));

        // 发送反馈
        OutputStream os = socket.getOutputStream();
        os.write("已接受到客户端数据！~".getBytes());


        // 释放资源
        serverSocket.close();
    }
}
